//
//  ZGLRecorderView.m
//  ZeegingLottery
//
//  Created by 李曌 on 2018/6/29.
//  Copyright © 2018年 蒂埃里. All rights reserved.
//

#import "ZGLRecorderView.h"
#import "ZGLLotteryRecorderCell.h"
#import "ZGLOrderModel.h"

#define sMenuViewH 50
#define sViewWidth self.bounds.size.width

@interface ZGLRecorderView ()<UITableViewDelegate,UITableViewDataSource>
{
    CGFloat btnW;
    
}

@property (strong, nonatomic)UIScrollView *menuScroller;
@property (strong, nonatomic)UIScrollView *contentScroller;
@property (strong, nonatomic)UIView *menuBgView;       //下滑杠

@property (strong, nonatomic)NSMutableArray *tableViewArray;
@property (strong, nonatomic)UITableView *refreshTableView;

@property (strong, nonatomic)NSArray *Data;;

@end



@implementation ZGLRecorderView
- (NSArray *)Data
{
    if (!_Data) {
        _Data = [NSArray array];
    }
    return _Data;
}

//- (void)setDataArr:(NSMutableArray *)dataArr
//{
//    _dataArr = dataArr;
//    
//}

- (UIScrollView *)menuScroller
{
    if (!_menuScroller) {
        _menuScroller = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, sMenuViewH)];
        _menuScroller.backgroundColor = [UIColor whiteColor];
        
    }
    return _menuScroller;
}

- (UIScrollView *)contentScroller
{
    if ((!_contentScroller)) {
        _contentScroller = [[UIScrollView alloc]initWithFrame:CGRectMake(0, sMenuViewH+5, self.bounds.size.width, self.bounds.size.height-sMenuViewH-5)];
        [_contentScroller setPagingEnabled:YES];
        [_contentScroller setScrollEnabled:NO];   //禁止手动滑动
    }
    return _contentScroller;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.menuScroller];
        [self addSubview:self.contentScroller];
        
        _tableViewArray = [[NSMutableArray alloc]init];
        [self setupContentView];
        [self refreshTableView:0];
    }
    
    return self;
}

- (void)setupContentView
{
    NSArray *titleArr = @[@"全部",@"待开奖",@"已中奖"];
//    NSArray *data = @[@[@"标签一",@"标签二",@"标签三",@"标签四",@"标签一",@"标签二",@"标签三",@"标签四",@"标签一",@"标签二",@"标签三",@"标签四",@"标签一",@"标签二",@"标签三",@"标签四",@"标签二",@"标签三",@"标签四"],@[@"标签五",@"标签六"],@[@"标签七",@"标签八",@"标签九",@"标签十"]];
//    //
//    _dataArr = [NSMutableArray arrayWithArray:data];
    
    btnW = self.bounds.size.width/titleArr.count;
    for (int i = 0; i<titleArr.count; i++) {
        UIButton *menu = [UIButton buttonWithType:UIButtonTypeCustom];
        [menu setFrame:CGRectMake(btnW * i, 0, btnW, _menuScroller.frame.size.height)];
        [menu setTitle:titleArr[i] forState:UIControlStateNormal];
        [menu setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        menu.titleLabel.font = [UIFont systemFontOfSize:15.0];
        menu.tag = i;
        [menu addTarget:self action:@selector(selectMenu:) forControlEvents:UIControlEventTouchUpInside];
        [_menuScroller addSubview:menu];
        
    }
    [_menuScroller setContentSize:CGSizeMake(_menuScroller.bounds.size.width, _menuScroller.frame.size.height)];
    _menuBgView = [[UIView alloc]initWithFrame:CGRectMake(btnW/4, _menuScroller.frame.size.height - 2, (btnW/2), 2)];
    [_menuBgView setBackgroundColor:globalRed];
    [_menuScroller addSubview:_menuBgView];
    
    //设置scrollview
    _contentScroller.contentSize = CGSizeMake(sViewWidth * titleArr.count, _contentScroller.frame.size.height);
    [self addTableViewToScrollView:_contentScroller count:titleArr.count frame:CGRectZero];
    
}

//
- (void)addTableViewToScrollView:(UIScrollView *)scrollView count:(NSUInteger)pageCount frame:(CGRect)frame {
    for (int i = 0; i < pageCount; i++) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(sViewWidth * i, 0 , sViewWidth, UI_SCREEN_HEIGHT - STATUEHEIGHT - sMenuViewH-5)];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.tag = i;
        [_tableViewArray addObject:tableView];
        [scrollView addSubview:tableView];
    }
}



- (void)selectMenu:(UIButton *)sender
{
    DLog(@"%@",sender.titleLabel.text);
    NSString *btnString = sender.titleLabel.text;
    if ([btnString isEqualToString:@"全部"]) {
        [TalkingData trackEvent:ZGLRecordListPageAllBtnEventID];
        
    }else if ([btnString isEqualToString:@"待开奖"]){
        [TalkingData trackEvent:ZGLRecordListPageWaitingLottBtnEventID];
        
    }else if ([btnString isEqualToString:@"已中奖"]){
        [TalkingData trackEvent:ZGLRecordListPageAlreadyLottBtnEventID];
    }
    
    [_contentScroller setContentOffset:CGPointMake(sViewWidth * sender.tag, 0) animated:YES];
    
    [self refreshTableView:(int)sender.tag];
    
    [self changeView:(int)sender.tag];
}

- (void)refreshTableView:(int)index {
    _refreshTableView = _tableViewArray[index];
    CGRect frame = _refreshTableView.frame;
    frame.origin.x = sViewWidth * index;
    [_refreshTableView setFrame:frame];
    
    //假数据
//    _Data = _dataArr[index];
//    [_refreshTableView reloadData];
    
//    if (index == 1) {
//        index = 4;
//    }else if (index == 2){
//        index = 5;
//    }
    
    [self getOrderListWithStatus:index];
    
    
}

//下滑杠
- (void)changeView:(float)x {
    
//    float xx = x * (btnW / sViewWidth);
    CGFloat xx = x * btnW + (btnW/4);
    [_menuBgView setFrame:CGRectMake(xx, _menuBgView.frame.origin.y, _menuBgView.frame.size.width, _menuBgView.frame.size.height)];
}

- (void)getOrderListWithStatus:(int )status
{
    //菊花
    [EasyLoadingView showLoadingText:@"" config:^EasyLoadingConfig *{
        return [EasyLoadingConfig shared].setLoadingType(LoadingShowTypeIndicator);
    }];
    
    NSString *token = sZGLUserInfoModel.token;
    NSString *Status = [NSString stringWithFormat:@"%d",status];
    if (status==0) {
        Status = @"all";
    }else if (status == 1){
        Status = @"toopen";
    }else{
        Status = @"already";
    }
    [sZGLHTTPRequestManager getOrderListWithStatus:Status Token:token Success:^(id responsedata) {
        
        DLog(@"%@",responsedata);
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [EasyLoadingView hidenLoading];
            self.Data = [ZGLOrderModel mj_objectArrayWithKeyValuesArray:responsedata];
            if (self.Data.count<=0) {
                [_refreshTableView setHidden:YES];
            }else{
                [_refreshTableView setHidden:NO];
                [_refreshTableView reloadData];
            }
            
        });
        
    } Fail:^(ZGLRespondStatusCode bef_status, NSString *bef_message, NSError *error) {
        
        DLog(@"%@",error);
        
    }];
    
    
}


#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.Data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZGLLotteryRecorderCell *cell = [ZGLLotteryRecorderCell LotteryRecorderCellWith:tableView];
    cell.model = _Data[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ZGLOrderModel *model = _Data[indexPath.row];
    NSString *url = model.url;
    
    if (self.indexPath) {
        self.indexPath(indexPath, url);
    }
    
}

@end
